package com.sq.goodline.util.http;

import java.util.Map;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class HttpClientUtil {
	@Autowired
    private RequestConfig requestConfig;
	
	@Autowired
	CloseableHttpClient httpClient;
	
	
	/**
	 * post json
	 * @param url
	 * @param jsonString
	 * @return
	 * @throws Exception
	 */
	public HttpResult doPostJson(String url,String jsonString) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(requestConfig);
		StringEntity stringEntity = new StringEntity(jsonString);
		httpPost.setEntity(stringEntity);
		httpPost.addHeader("Content-Type", "application/json");

		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpClient.execute(httpPost);
			return new HttpResult(response.getStatusLine().getStatusCode(),
					EntityUtils.toString(response.getEntity(), "UTF-8"));
		} finally {
			if (response != null) {
				response.close();
			}
			// httpclient.close();//不关闭，交给连接池管理
		}
	}
	
	/**
	 * http post调用
	 * 可通过设置http head，发起post json、xml以及web service调用
	 * @param httpRequest
	 * @return
	 * @throws Exception
	 */
	public HttpResult doHttpPost(HttpRequest httpRequest) throws Exception {
		HttpPost httpPost = new HttpPost(httpRequest.getUrl());
		httpPost.setConfig(requestConfig);
		StringEntity stringEntity = new StringEntity(httpRequest.getRequestBody());
		httpPost.setEntity(stringEntity);

		// 设置header信息
		Map<String, String> headMap = httpRequest.getHeadMap();
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}

		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpClient.execute(httpPost);
			return new HttpResult(response.getStatusLine().getStatusCode(),
					EntityUtils.toString(response.getEntity(), "UTF-8"));
		} finally {
			if (response != null) {
				response.close();
			}
			// httpclient.close();//不关闭，交给连接池管理
		}
	}
	
	//指定charset
	public HttpResult doHttpPost(HttpRequest httpRequest,String charset) throws Exception {
		HttpPost httpPost = new HttpPost(httpRequest.getUrl());
		httpPost.setConfig(requestConfig);
		StringEntity stringEntity = new StringEntity(httpRequest.getRequestBody(),charset);
		httpPost.setEntity(stringEntity);

		// 设置header信息
		Map<String, String> headMap = httpRequest.getHeadMap();
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}

		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpClient.execute(httpPost);
			return new HttpResult(response.getStatusLine().getStatusCode(),
					EntityUtils.toString(response.getEntity(), charset));
		} finally {
			if (response != null) {
				response.close();
			}
			// httpclient.close();//不关闭，交给连接池管理
		}
	}

}
